package com.xingzhi.kotlindemo

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener

class MainActivity : AppCompatActivity(), OnClickListener {
    private var viewPager: ViewPager? = null
    private var pageList: MutableList<View> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewPager = findViewById<ViewPager>(R.id.viewPager)
        pageList = ArrayList()

        // 添加页面
        val inflater = LayoutInflater.from(this)
        for (i in 0..1) {
            val page: View = inflater.inflate(R.layout.page_item, null)
            val textView = page.findViewById<TextView>(R.id.page_text)
            textView.text = "Page " + (i + 1)
            pageList.add(page)
            page.setOnClickListener(this)
        }

        // 设置 ViewPager 的适配器
        val adapter: PagerAdapter = object : PagerAdapter() {
            override fun getCount(): Int {
                return pageList.size
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean {
                return view === `object`
            }

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                val page = pageList.get(position)
                container.addView(page)
                return page
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(`object` as View)
            }
        }
        viewPager?.setAdapter(adapter)

        // 设置 ViewPager 的属性
        viewPager?.setClipToPadding(false)
        viewPager?.setPadding(0, 0, 100, 0) // 右侧留出 100px 用于展示下一页
        viewPager?.setPageMargin(20) // 设置页面之间的间距
        viewPager?.setOffscreenPageLimit(2) // 预加载的页面数量

        // 监听页面切换事件
        // 监听页面切换事件
        viewPager?.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                if (position == pageList.size - 1) {
                    // 最后一页，去除右侧 padding
                    viewPager?.setPadding(100, 0, 0, 0)
                } else {
                    // 非最后一页，恢复右侧 padding
                    viewPager?.setPadding(0, 0, 100, 0)
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
    }

    override fun onClick(v: View?) {
        startActivity(Intent(this, PagerActivity::class.java))
    }
}